package com.unlcn.ils.kas.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.unlcn.ils.kas.annotation.LogRecord;
import com.unlcn.ils.kas.dao.model.sysadmin.SysLog;
import com.unlcn.ils.kas.service.bo.SysLogBo;
import com.unlcn.ils.kas.service.sysadmin.SysLogService;
import com.unlcn.ils.kas.vo.RestVO;
import com.unlcn.ils.kas.vo.SysLogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 操作日志接口
 * 
 * @author 罗必量
 */
@RestController
@RequestMapping("/log")
public class LogController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogController.class);

    @Autowired
    private SysLogService sysLogService;

    /**
     * 操作日志分页列表
     * 
     * @param page
     * @return
     */
    @PostMapping(value = "/queryPage", consumes = "application/json")
    @ResponseBody
    public RestVO<Page<SysLogVO>> queryPage(@RequestBody Page<SysLogBo> page) {
        RestVO<Page<SysLogVO>> result = new RestVO<>(0, "success");
        try {
            Page<SysLogVO> pageLogVO = new Page<>();
            Page<SysLog> returnPage = sysLogService.queryLogPage(page);
            BeanUtils.copyProperties(returnPage, pageLogVO);
            List<SysLog> logList = returnPage.getRecords();
            List<SysLogVO> logVOList = new ArrayList<SysLogVO>();
            for (SysLog sysLog : logList) {
                SysLogVO sysLogVO = new SysLogVO();
                BeanUtils.copyProperties(sysLog, sysLogVO);

                if (sysLog.getStartTime() != null) {
                    // 格式化操作时间
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    sysLogVO.setStartTime(format.format(sysLog.getStartTime()));
                }

                logVOList.add(sysLogVO);
            }
            pageLogVO.setRecords(logVOList);

            result.setData(pageLogVO);
        } catch (Exception ex) {
            LOGGER.error("LogController.deleteLog error: {}", ex);
            result.setCode(-1);
            result.setMessage("error");
        }
        return result;
    }

    /**
     * 删除操作日志
     * 
     * @param ids
     * @return
     */
    @LogRecord(description = "删除操作日志")
    @PostMapping(value = "/deleteLog", consumes = "application/json")
    @ResponseBody
    public RestVO<Object> deleteLog(@RequestBody Long[] ids) {
        RestVO<Object> result = null;
        try {
            Integer count = sysLogService.deleteLog(Arrays.asList(ids));
            if (count > 0) {
                result = new RestVO<Object>(0, count, "success");
            } else {
                result = new RestVO<Object>(0, count, "false");
            }
        } catch (Exception ex) {
            LOGGER.error("LogController.deleteLog error: {}", ex);
            result.setCode(-1);
            result.setMessage("error");
        }
        return result;
    }
}
